/*
 * @Author: 李无敌
 * @Date: 2025-07-25 13:57:38
 * @LastEditors: 李无敌
 * @LastEditTime: 2025-08-07 17:27:49
 * @FilePath: \nest-base\src\app.module.ts
 */
/*
 * @Author: 李无敌
 * @Date: 2025-07-25 13:57:38
 * @LastEditors: 李无敌
 * @LastEditTime: 2025-08-06 16:02:00
 * @FilePath: \nest-base\src\app.module.ts
 */
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ThrottlerModule } from '@nestjs/throttler';
import { ScheduleModule } from '@nestjs/schedule';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { BullModule } from '@nestjs/bull';
import { TerminusModule } from '@nestjs/terminus';
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE, APP_GUARD } from '@nestjs/core';

// 导入配置
import configuration from './config/configuration';
import { createDatabaseConfig } from './config/database.config';

// 导入全局提供者
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import { UserLogInterceptor } from './common/interceptors/user-log.interceptor';
import { ValidationPipe } from './common/pipes/validation.pipe';
import { GlobalAuthGuard } from './common/guards/global-auth.guard';

// 导入共享服务
import { RedisService } from './shared/redis/redis.service';
import { LoggerService } from './shared/logger/logger.service';

// 导入 UserLogService 和 UserLog 实体
import { UserLogService } from './modules/user-log/services/user-log.service';
import { UserLog } from './modules/user-log/entities/user-log.entity';
import { Role } from './modules/role/entities/role.entity';

// 导入模块
import { AuthModule } from './modules/auth/auth.module';
import { UserModule } from './modules/user/user.module';
import { UserLogModule } from './modules/user-log/user-log.module';
import { RoleModule } from './modules/role/role.module';
import { FileModule } from './modules/file/file.module';
import { SecurityLogModule } from './modules/security-log/security-log.module';
import { SchedulerModule } from './modules/scheduler/scheduler.module';

// 导入API文档控制器
import { ApiDocsController } from './controllers/api-docs.controller';

@Module({
  imports: [
    // 配置模块
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration],
    }),

    // 数据库模块
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => createDatabaseConfig(configService),
      inject: [ConfigService],
    }),
    // 添加 TypeOrmModule.forFeature([UserLog, Role])
    TypeOrmModule.forFeature([UserLog, Role]),

    // 限流模块
    ThrottlerModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        throttlers: [{
          ttl: configService.get('security.rateLimit.windowMs'),
          limit: configService.get('security.rateLimit.max'),
        }],
      }),
      inject: [ConfigService],
    }),

    // 任务调度模块
    ScheduleModule.forRoot(),

    // 事件发射器模块
    EventEmitterModule.forRoot(),

    // 队列模块
    BullModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        redis: {
          host: configService.get('redis.host'),
          port: configService.get('redis.port'),
          password: configService.get('redis.password'),
          db: configService.get('redis.db'),
        },
      }),
      inject: [ConfigService],
    }),

    // 健康检查模块
    TerminusModule,

    // 业务模块
    AuthModule,
    UserModule,
    UserLogModule,
    RoleModule,
    FileModule,
    SecurityLogModule,
    SchedulerModule
  ],
  controllers: [
    // API文档控制器
    ApiDocsController,
  ],
  providers: [
    // 全局过滤器
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
    // 暂时注释掉TransformInterceptor，避免TypeORM实体序列化问题
    // {
    //   provide: APP_INTERCEPTOR,
    //   useClass: TransformInterceptor,
    // },
    {
      provide: APP_INTERCEPTOR,
      useClass: UserLogInterceptor,
    },
    // 全局管道
    {
      provide: APP_PIPE,
      useClass: ValidationPipe,
    },
    // 全局认证守卫
    {
      provide: APP_GUARD,
      useClass: GlobalAuthGuard,
    },
    // 共享服务
    RedisService,
    LoggerService,
    // 添加 UserLogService 到 providers 中
    UserLogService,
  ],
  exports: [RedisService, LoggerService],
})
export class AppModule {}
